package executor.impl;
import java.io.File;
import java.util.Random;
import java.util.concurrent.BlockingDeque;

public class Indexer implements Runnable {
	private final BlockingDeque<File> queue;
	private final BlockingDeque<File>[] queues;
	private final Random rnd;

	public Indexer(BlockingDeque<File> queue, BlockingDeque<File>[] queues) {
		this.queue = queue;
		this.queues = queues;
		rnd = new Random();
	}

	public void run() {
		File f = queue.poll();
		if (f == null) {
			int n = rnd.nextInt(queues.length);
			f = f == null ? queues[n].pollLast() : f;
		}
		if (f != null)
			indexFile(f);
	}

	private void indexFile(File f) {
		System.out.println(f.getAbsolutePath());
	}
}